
using Microsoft.VisualStudio.Modeling;
using Microsoft.VisualStudio.Modeling.Diagrams;

namespace Dslfactory.wcfLanguage
{
 
 /// <summary>
 /// Rule changing an Interface into a ServiceContract and vice-versa
 /// for interfaces into the project, and IsInterfaceContact is changed
 /// </summary>
 [RuleOn(typeof(Interface))]
 public class Interface_ServiceContract_Transformations : ChangeRule
 {
  public override void ElementPropertyChanged(ElementPropertyChangedEventArgs e)
  {
   if (e.DomainProperty.Id == Interface.IsServiceContractDomainPropertyId)
   {
    Interface iface = e.ModelElement as Interface;
    if (iface.IsServiceContract && (iface is ServiceContract))
     return;
    if ((!iface.IsServiceContract) && (!(iface is ServiceContract)))
     return;

    if (PresentationViewsSubject.GetPresentation(iface).Count == 0)
     return;

    PresentationElement shape = PresentationViewsSubject.GetPresentation(iface)[0];
    PresentationViewsSubject link = PresentationViewsSubject.GetLink(shape, iface);
    Interface serviceContract;
    if (iface.IsServiceContract)
     serviceContract = new ServiceContract(iface.Store);
    else
     serviceContract = new Interface(iface.Store);

    while (iface.Methods.Count > 0)
    {
     Method m = iface.Methods[0];
     m.Interface = serviceContract;
    }

     serviceContract.FullName = iface.FullName;
     serviceContract.Name = iface.Name;
     serviceContract.Namespace = iface.Namespace;
     serviceContract.IsServiceContract = iface.IsServiceContract;
     serviceContract.ServicesDefinition = iface.ServicesDefinition;

     link.Subject = serviceContract;
    
     iface.Delete();
    }
  }
 }
}
